-- room.lua
-- 坦克对战房间。处理对战。

local Room = {
    type = "TanksRoom",
}

--[[
Room 数据结构
{
	type = "TanksRoom",

	player1 = Player,
	player2 = Player,
}
--]]

local log = require("log"):new("tanks.room")
local pb = require("protobuf")
local Player = require("tanks.player")

-- 创建房间
function Room:new(player1, player2)
    assert(player1.account)
    assert(player2.account)
    -- 按名字排编号
    if player2.account < player1.account then
        player1, player2 = player2, player1
    end

    assert(player1.account <= player2.account)
    log:debug("Create room")
    local room = {
        player1 = player1,
        player2 = player2,
        msg_list = {},
        tmp_msg_list = {},
    }

    setmetatable(room, self)
    self.__index = self

    player1:enter_room(player2.account, false)
    player2:enter_room(player1.account, true)
    return room
end  -- new()

-- 取房间中另一玩家
local function get_another_player(room, game_clt_id)
    assert(room)
    assert(game_clt_id)
    local player1 = room.player1;
    local player2 = room.player2;
    assert(player1 and player2)
    if game_clt_id:equals(player1.game_clt_id) then
        return player2
    end
    assert(game_clt_id:equals(player2.game_clt_id))
    return player1
end  -- get

--获取房间中的当前玩家
local function get_current_player(room,game_clt_id)
    assert(room)
    assert(game_clt_id)
    local player1 = room.player1
    local player2 = room.player2
    assert(player1 and player2)
    if game_clt_id:equals(player1.game_clt_id) then
        return player1
    end
    assert(game_clt_id:equals(player2.game_clt_id))
    return player2
end    

-- 玩家的移动
function Room:player_move_to(game_clt_id, position_msg)
    log:info("player_move_to()")
    local current_player = get_current_player(self,game_clt_id)
    local frame_msg = {user_id = current_player.account,position = position_msg}
    table.insert(self.msg_list,frame_msg)
    table.insert(self.tmp_msg_list,frame_msg)
end

-- 玩家的旋转
function Room:player_rotate_to(game_clt_id, rotation_msg)
    local current_player = get_current_player(self,game_clt_id)
    local frame_msg = {user_id = current_player.account,rotation = rotation_msg}
    table.insert(self.msg_list,frame_msg)
    table.insert(self.tmp_msg_list,frame_msg)
end

-- 开火
function Room:player_fire_to(game_clt_id, fire_msg)
    local another_player = get_another_player(self, game_clt_id)
    another_player:fire_msg(fire_msg)
end

--爆炸
function Room:shell_explode_to(game_clt_id,explosion_msg)
    log:debug("room:shell_explode_to")
    --local another_player = get_another_player(self, game_clt_id)
    local player1 = self.player1
    local player2 = self.player2

    --伤害计算
    local explosion_position = explosion_msg.position;
    local damage1,damage2
    damage1,damage2 = self:calculate_damage(explosion_position,game_clt_id)

    local damage_msg1 = {tank1 = damage2,tank2 = damage1}
    local damage_msg2 = {tank1 = damage1,tank2 = damage2};

    log:debug("damage %f,%f",damage1,damage2)

    player1:explode_msg(damage_msg1)
    player2:explode_msg(damage_msg2)
end

function Room:calculate_damage(explosion_pos,game_clt_id)
    log:debug("room:calculate_damage")
    local player1 = self.player1
    local player2 = self.player2

    log:debug("player1=%f,%f,%f",player1.x,player1.y,player1.z)
    log:debug("player2=%f,%f,%f",player2.x,player2.y,player2.z)

    local explosion_to_p1 = math.sqrt(math.pow((explosion_pos.x - player1.x),2) + 
        math.pow((explosion_pos.y - player1.y),2) + math.pow((explosion_pos.z - player1.z),2))
    local explosion_to_p2 = math.sqrt(math.pow((explosion_pos.x - player2.x),2) + 
        math.pow((explosion_pos.y - player2.y),2) + math.pow((explosion_pos.z - player2.z),2))

    local relative_distance1 = (5.0 - explosion_to_p1) / 5.0
    local relative_distance2 = (5.0 - explosion_to_p2) / 5.0

    local damage1 = relative_distance1 * 100.0
    local damage2 = relative_distance2 * 100.0
    if damage1 < 0 then damage1 = 0 end
    if damage2 < 0 then damage2 = 0 end

    return damage1,damage2
end


function Room:on_disconnected()
    msg_list = nil
end

return Room
